// pages/upload_userinfo/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        avatarUrl: '', // 头像
        name: '', // 姓名
        duties: '', // 职务
        bindPhone: '', // 绑定手机号
        email: '',  // 邮箱
        addres: '' // 地址
    },
    // //选择头像
    onChooseAvatar(e) {
        var that = this;
        const {
            avatarUrl
        } = e.detail;
        wx.getImageInfo({
            src: e.detail.avatarUrl,
            success(res) {
                console.log(res);
                if (res.width !== res.height) { //如果不是正方形 进行裁剪工作
                    //微信裁剪接口（2.26.0 开始支持）
                    wx.cropImage({
                        src: e.detail.avatarUrl, // 图片路径
                        cropScale: '1:1', // 裁剪比例
                        success(res) {
                            console.log(res)
                            getApp().util.uploadfile(res.tempFilePath, {}).then((res) => {
                                let src = res.data.fullurl
                                that.setData({
                                    avatarUrl: src,
                                });
                            })
                        },
                        fail: function (res) { },
                        complete: function (res) { }
                    })

                } else {
                    getApp().util.uploadfile(avatarUrl, {}).then((res) => {
                        let src = res.data.fullurl
                        that.setData({
                            avatarUrl: src,
                        });
                    })
                }
            },
            fail: function (res) { },
            complete: function (res) { }
        })
    },

    //授权手机号
    getPhoneNumber: function (e) {
        console.log(e);
        var that = this;
        if (e.detail.errMsg == "getPhoneNumber:ok") {
            var params = {}
            params.encryptedData = e.detail.encryptedData
            params.iv = e.detail.iv
            params.code = e.detail.code;

            getApp().util.request({
                url: 'user/get_mobile',
                data: params,
            }).then(function (res) {
                console.log(res)
                that.setData({
                    bindPhone: res.phone,
                })
            })
        }else{
            // wx.showToast({
            //   title: e.detail.errMsg,
            //   icon:'none'
            // });
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            avatarUrl: options.img
        })
        this.init();
    },
    init() {
		return new Promise(async (resolve, reject) => {

			var user = await getApp().util.getUser();
			
			this.setData({
                id:user.userInfo.id,
				duties: user.userInfo.position,
				bindPhone:user.userInfo.mobile,
				email:user.userInfo.email,
				name:user.userInfo.username,
                avatarUrl:user.userInfo.avatar,
                addres:user.userInfo.company_address,
			}, () => {
				resolve();
			})
		})
	},
    handler() { },
    onInput(e) {
        var userInfot = {
            // id:this.data.id,
            username: this.data.name,
            avatar: this.data.avatarUrl,
            address: this.data.addres,
            position: this.data.duties,
            mobile: this.data.bindPhone,
            email: this.data.email,
        };
        console.log(userInfot);
        if (userInfot.avatar == '') {
            wx.showToast({
                title: '请选择头像',
                icon: 'none'
            })
            return;
        } 
        if (userInfot.username == '') {
            wx.showToast({
                title: '请填写姓名',
                icon: 'none'
            })
            return;
        } 
        if (userInfot.position == '') {
            wx.showToast({
                title: '请填写职务',
                icon: 'none'
            })
            return;
        } 
        if (userInfot.mobile == '') {
            wx.showToast({
                title: '请授权手机号',
                icon: 'none'
            })
            return;
        } 
        if (userInfot.email == '') {
            wx.showToast({
                title: '请填写邮箱',
                icon: 'none'
            })
            return;
        } 
        let params = userInfot;
        
        getApp().util.request({
            url: 'user/update_user',
            data: params,
        }).then(function (res) {
            wx.showToast({
                title: '保存成功',
                icon: 'success'
            })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})